package com.mengxuegu.springboot.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mengxuegu.springboot.entities.Bill;
import com.mengxuegu.springboot.mapper.BillMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
public class BillController {
    @Autowired
    BillMapper billMapper;

    @GetMapping("/bill/{bid}")
    public Bill getBill(@PathVariable("bid") Integer bid) {
        Bill bill = billMapper.getBillByBid(bid);
        return bill;
    }

    @PostMapping("/bill")
    public Bill addBill(@RequestBody Bill bill) {
        int i = billMapper.addBill(bill);
        System.out.println(i);
        return bill;
    }

    @GetMapping("/bill")
    public PageInfo<Bill> getBill() {
        // 两个信息存在于ThreadLocal中
        PageHelper.startPage(1,2);
        // 这里的List实际是个Page
        List<Bill> allBill = billMapper.getAllBill();
        // Page实际是个List，继承了ArrayList
        List<Bill> aa = new Page<>();
        return new PageInfo<>(allBill);
    }
}
